$(document).ready(function() {
    $("#buttonlogin").click(function() {
        $('#show_seach').hide();
        $('#show_login').fadeToggle();

    });
    $("#btnshow_search").click(function() {
        $('#show_seach').fadeToggle();
    });

    $('.product_large').hover(
            function() {

                $(this).find('.images_hover').fadeIn();
            },
            function() {
                $(this).find('.images_hover').fadeOut();
            }
    );
    $('.product_small').hover(
            function() {
                $(this).find('.images_hover_small').fadeIn();
            },
            function() {
                $(this).find('.images_hover_small').fadeOut();

            }
    );
    $('.product_small').hover(
            function() {

                $(this).find('.images_hover_small').fadeIn();
            },
            function() {
                $(this).find('.images_hover_small').fadeOut();
            }
    );
    $('.product_avg').hover(
            function() {

                $(this).find('.images_hover_avg').fadeIn();
            },
            function() {
                $(this).find('.images_hover_avg').fadeOut();
            }
    );
    $('.product_small_small').hover(
            function() {

                $(this).find('.images_hover_small_small').fadeIn();
            },
            function() {
                $(this).find('.images_hover_small_small').fadeOut();
            }
    );

    var div = $('#topmenu_red');
    var start = $(div).offset().top;

    $.event.add(window, "scroll", function() {
        var p = $(window).scrollTop();
        $(div).css('position', ((p) > start) ? 'fixed' : 'static');
        $(div).css('top', ((p) > start) ? '0px' : '');
    });

    /* son*/
   $("#tong1").hide();
   $("#cbbgiaohang").change(function (){
       var loai=$(this).val();
     
       var tong=0;
      
       if(loai==50000) {$("#tienvanchuyen").html("50.000 VNĐ"); tong=$("#tong1").text();   }
       else
       if(loai==0) {tong=$("#tongtien").text(); $("#tienvanchuyen").html("0 VNĐ");}
       $("#tongcongchiphi").html(tong);
      // $("#tongcongchiphi")
   })
    $(".add_to_cart").click(function() {
        $x = $(this).parent().parent().parent().parent().parent();
        //  alert($x.html());
        var name = $x.find('p').html();
        var price = $x.find('span').text().replace('đ', '');
        var qty = 1;
        var id = $x.find('.muiten').attr("id") + $x.find('.muiten_small').attr("id");// .text() + $x.find('.muiten_small').text();
        id = id.replace('undefined', '');
        //alert(id+name+price);
        $.ajax({
            type: "GET",
            url: globalurl + 'index.php/carts/add',
            data: {
                id: id,
                qty: qty,
                name: name,
                price: price
            },
            success: function(data)
            {
                $("#danhsachsp").html(data);
            }
        });
    });
    $("#giohang").hover(function() {
        $("#danhsachsp").fadeIn(1000);
        if ($.trim($("#danhsachsp").text()) == "")
            $.ajax({
                type: "GET",
                url: globalurl + 'index.php/carts/show',
                data: {
                },
                success: function(data)
                {
                    $("#danhsachsp").html(data);
                }
            });
    });
    $('.btn_exit').click(function() {
        $('#bg_edit_product').hide();
    });

    $("#btnmuangay").click(function() {
        var id = $(this).attr('class');
        var name = $(".product_title").text();
        var price = $(".sell_price").text();
        price = price.replace("đ", "");
        //  alert(id+name+price);
        $.ajax({
            type: "GET",
            url: globalurl + 'index.php/carts/add',
            data: {
                id: id,
                qty: 1,
                name: name,
                price: price
            },
            success: function(data)
            {
                window.location = globalurl + "index.php/carts/"

            }
        });
    });
    $(".removeitem").click(function() {
        if (confirm('Bạn có muốn xóa sản phẩm này không?')) {
            $rowid = $(this).attr("id");
            $.ajax({
                type: "GET",
                url: globalurl + 'index.php/carts/update',
                data: {
                    rowid: $rowid,
                    qty: 0,
                    
                },
                success: function(data)
                {
                    location.reload();
                }
            });
        }
    });


    //Ajax Thao

    $("#username").blur(function() {
        $.ajax({
            type: "GET",
            url: globalurl + 'index.php/welcome/kiemtra_username',
            data: {
                name_user: $('#username').val()
            },
            success: function(data) {
                if (data.indexOf("true") > 0)
                {
                    $("#username_hint").html('Tên đăng nhập đã tồn tại!');
                    $("#username_hint").show();
                    $("#username").focus();
                }
                else
                {
                    $("#username_hint").html('');
                    $("#username_hint").hide();
                }


            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError);
            }
        });
    });

    $("#email").blur(function() {
        $.ajax({
            type: "GET",
            url: globalurl + 'index.php/welcome/kiemtra_email',
            data: {
                email_user: $('#email').val()
            },
            success: function(data) {
                if (data.indexOf("true") > 0)
                {
                    $("#email_hint").html('Email này đã được sử dụng!');
                    $("#email_hint").show();
                    $("#email").focus();
                }
                else
                {
                    $("#email_hint").html('');
                    $("#email_hint").hide();
                }

            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError);
            }
        });
    });
    $("#basket .continue").click(function() {
	var loai= $("#cbbgiaohang").val();
	 if(loai==50000)
	 {
		 window.location = globalurl + "index.php/carts/address?phi=50000";
	 }
     else   window.location = globalurl + "index.php/carts/address?phi=0";
    })

   $('.details-link').click(function(e) {
		e.preventDefault();
		var id_details=$(this).attr("href");
        $.ajax({
            type: "POST",
            url: globalurl+ 'index.php/welcome/chitiethd',
            data: {
                id_details: id_details               
            },
            success: function(data) {
                $('#p-details').html(data);
				$('#p-details').toggle( "slow" );
            }
        });     
		
	}); 

	$('#btn_login').click(function(e) {
		e.preventDefault();	
		var user_email=$('#input1').val();
		var pass = $('#input2').val();
        $.ajax({
            type: "POST",
            url: globalurl+ 'index.php/welcome/kiemtra_dangnhap',
            data: {
                user_email: user_email,
				pass: pass
            },
            success: function(data) {
				if(data.indexOf("tên")>0)
				{
					alert(data);
				}
				else location.reload();
            }
        }); 		
		
		
	});
	
    $("#current_pass").blur(function() {
        $.ajax({
            type: "GET",
            url: globalurl + 'index.php/welcome/kiemtra_pass',
            data: {
                current_pass: $('#current_pass').val()
            },
            success: function(data) {
                if (data.indexOf("false") > 0)
                {
                    $("#current_hint").html('Mật khẩu hiện tại không đúng!');
                    $("#current_hint").show();
                    $("#current_pass").focus();
                }
                else
                {
                    $("#current_hint").html('');
                    $("#current_hint").hide();
                }


            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError);
            }
        });
    });	
	
	$("#btn_changepass").click(function(e) {
		e.preventDefault();	
        $.ajax({
            type: "GET",
            url: globalurl + 'index.php/welcome/change_pass',
            data: {
                newpassword: $('#newpassword').val()
            },
            success: function(data) {
				alert(data);				
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError);
            }
        });
    });	

});